/*
Author : Nguyen Van Ty
Email  : ngvanty03@gmail.com
Date   : 4/7/2011 

*/

using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Utility;
using BusinessObject;
using System.Xml;

namespace DataAccessLayer
{
    /// <summary>
    /// Define HinhAnhMatHangDao class. This class inherit from  HinhAnhMatHangDaoBase class.
    /// If you want add new function, you must define here.
    /// </summary>
    public class HinhAnhMatHangDao : HinhAnhMatHangDaoBase
    {
        private static ILog logger = null;
        #region Constructor
        /// <summary>
        /// Define Constructor.
        /// </summary>
        public HinhAnhMatHangDao() { }
        static HinhAnhMatHangDao()
        {
            // Init logger.
            logger = LoggingUtility.GetLogger(typeof(HinhAnhMatHangDao).FullName);
        }
        public static List<HinhAnhMatHangResult> GetAllByMatHang(string maMH)
        {
            String methodName = "GetAllByMatHang";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                List<HinhAnhMatHangResult> listHinhAnhMatHang = new List<HinhAnhMatHangResult>();
                DbCommand dbCommand = ManagerDao.GetStoredProcCommand("spGetAllHinhAnhMatHangByMaMH");
                ManagerDao.AddInParameter(dbCommand, "MaMatHang", DbType.String,maMH);
                using (IDataReader reader = ManagerDao.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        listHinhAnhMatHang.Add(GetHinhAnhMatHangResultByDataReader(reader));
                    }
                    reader.Close();
                }
                return listHinhAnhMatHang;
            }
            catch (Exception ex)
            {
                //write error message to log file.
                logger.Error(methodName, ex);
                return null;
            }
        }
        public static bool Delete(string MaMH)
        {
            String methodName = "DeleteHinhAnhMatHangByMaMH";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                DbCommand dbCommand = ManagerDao.GetStoredProcCommand("spDeleteHinhAnhMatHangByMaMatHang");
                //Add value "Id"  paramater.
                ManagerDao.AddInParameter(dbCommand, "MaMatHang", DbType.String, MaMH);
                //Add value returnValue  paramater.
                ManagerDao.AddParameter(dbCommand, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
                try
                {
                    ManagerDao.ExecuteNonQuery(dbCommand);
                    int parameterValue = (int)ManagerDao.GetParameterValue(dbCommand, "returnValue");
                    if (parameterValue == 1)
                    {
                        return true;
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                return false;
            }
            catch (Exception e)
            {
                //write error message to log file.
                logger.Error(methodName, e);
                return false;
            }
        }
    }
}
        #endregion